<!--注册功能-->
<script>
export default {
  data() {
    datalist:[];
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      }
    };
    return {
      ruleForm: {
        id: '',
        name:'',
        pass: '',
        checkPass: ''
      },
      temp:true
      ,
      mes:''
      ,
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        id: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 5, max: 12, message: '长度在 5 到 12 个字符', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]+$/, message: '账号只能包含字母和数字', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    checkId(id){
      this.$http.post("/sign","mark=check&id="+id).then((resp)=>{
        if(resp.data.code == 200){
          this.mes = "该账号已被注册!";
          this.temp = false;
          return;
        }else if(resp.data.code == 201){
          this.mes = "该账号没有被注册!";
          this.temp = true;
          return;
        }
      })
    },
    submitForm(formName) {
      this.$http.post("/sign","mark=register&"+jsonToString(this.ruleForm)).then((resp)=>{
        if(resp.data.code == 200){
          this.$message({
            message: '注册成功!',
          })
          this.$router.push('/login');
        }else{
          this.$message({
            message: '注册失败!',
            type: 'error'
          })
        }
      });
    },
    resetForm(formName) {
    //   情况表单内容
      this.$refs[formName].resetFields();
    }
  }
}
function jsonToString(form) {
  var str = "";
  for (var s in form) {
    str += s + "=" + form[s] + "&";
  }
  return str.substring(0, str.length - 1);
}
</script>

<template>
  <div class="login_container">
		<span style="font-size:45px ;font-family:宋体; font-weight: 800; position: absolute;
			left: 34%;top: 14%;color: black;
		 ">请注册你的账号</span>
    <!-- 登录盒子-->
    <div class="login_box2">
      <!-- 登录表单-->
      <div style="margin-top: 100px ; padding-right: 20px;">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="账号" prop="id">
            <el-input type="text" v-model="ruleForm.id" autocomplete="off" @blur = "checkId(ruleForm.id)"></el-input>
            {{mes}}<br>
          </el-form-item>
          <el-form-item label="用户姓名" prop="name">
            <el-input type="text" v-model="ruleForm.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" status-icon :rules="rules" prop="pass">
            <el-input type="password" status-icon :rules="rules" v-model="ruleForm.pass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm(ruleForm)">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

    </div>
  </div>
</template>

<style>
  .login_box2{
    width: 450px;
    height: 550px;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    opacity: 0.85;
  }
</style>